package com.beiye.drivertransport.accident.training;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.frame.adapter.CommonAdapter;
import com.android.frame.adapter.ViewHolder;
import com.android.frame.http.HttpListener;
import com.beiye.drivertransport.R;
import com.beiye.drivertransport.SubActivity.AccidentTrainingCourseDetalisActivity;
import com.beiye.drivertransport.SubActivity.SpecifiedLearningCourseDetailsAgainActivity;
import com.beiye.drivertransport.activity.TwoBaseAty;
import com.beiye.drivertransport.bean.CheckBean;
import com.beiye.drivertransport.bean.LoginUserBean;
import com.beiye.drivertransport.bean.SpecifiedLearningCourseBean;
import com.beiye.drivertransport.bean.SpecifiedPhotoBean;
import com.beiye.drivertransport.config.AppInterfaceConfig;
import com.beiye.drivertransport.http.Login;
import com.beiye.drivertransport.utils.CameraCanUseUtils;
import com.beiye.drivertransport.utils.LogUtils;
import com.beiye.drivertransport.utils.TiShiDialog;
import com.beiye.drivertransport.utils.ToastUtil;
import com.beiye.drivertransport.utils.UserManger;
import com.beiye.drivertransport.utils.Utils;
import com.githang.statusbar.StatusBarCompat;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.squareup.picasso.Picasso;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class SubAccidentTrainingActivity extends TwoBaseAty {

    ImageView img_safeback;

    ListView lv_course;

    View empty_view;

    TextView tv_title;
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE"};
    ArrayList<SpecifiedLearningCourseBean.RowsBean> courseList = new ArrayList<>();
    private AccidenttrainingAdapter accidenttrainingAdapter;
    ArrayList<SpecifiedLearningCourseBean.RowsBean> courseList1 = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_sub_accident_training;
    }

    public void onClick(View view) {
        if (view.getId() == R.id.img_safeback) {
            finish();
        }
    }

    @Override
    public void initData() {
        img_safeback = (ImageView) findViewById(R.id.img_safeback);
        lv_course = (ListView) findViewById(R.id.lv_course);
        empty_view = (View) findViewById(R.id.empty_view);
        tv_title = (TextView) findViewById(R.id.tv_title);
        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#1F80C4"));
        initUi();
    }

    private void initUi() {

        lv_course.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                //检测是否有写的权限
                int permission = ActivityCompat.checkSelfPermission(SubAccidentTrainingActivity.this,
                        "android.permission.WRITE_EXTERNAL_STORAGE");
                if (permission != PackageManager.PERMISSION_GRANTED) {
                    // 没有写的权限，去申请写的权限，会弹出对话框
                    ActivityCompat.requestPermissions(SubAccidentTrainingActivity.this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
                    return;
                }
                if (!CameraCanUseUtils.isCameraCanUse()) {

                    ToastUtil.showShortToast(SubAccidentTrainingActivity.this, "请到手机设置界面里找驾运宝允许开启照相");
                } else {
                    if (accidenttrainingAdapter.getItem(position).getMark() == 0) {
                        showLoadingDialog("");
                        int tcSn = accidenttrainingAdapter.getItem(position).getTcSn();
                        int otcSn = accidenttrainingAdapter.getItem(position).getOtcSn();
                        int sn = accidenttrainingAdapter.getItem(position).getSn();
                        int readLength = accidenttrainingAdapter.getItem(position).getReadLength();
                        courseList1.clear();
                        for (int i = 0; i < courseList.size(); i++) {
                            if (courseList.get(i).getOtcSn() != otcSn && courseList.get(i).getMark() != 1) {
                                courseList1.add(courseList.get(i));
                            }

                        }
                        Bundle bundle = new Bundle();
                        bundle.putInt("tcsn", tcSn);
                        bundle.putInt("otcSn", otcSn);
                        bundle.putInt("sn", sn);//用户课程序列号
                        bundle.putInt("readLength", readLength);//保存时间
                        bundle.putSerializable("CourseDetailList", (Serializable) courseList1);
                        SharedPreferences sp = getSharedPreferences("SubAccidentTrainingActivity",
                                Context.MODE_PRIVATE);
                        String orgId = sp.getString("orgId", "");
                        //    int minPer = sp.getInt("minPer", 0);
                        //     int photoMark = sp.getInt("photoMark", 0);
                        //     int signMark = sp.getInt("signMark", 0);
                        int learnsn = sp.getInt("learnsn", 0);
                        //     int faceRecgMark = sp.getInt("faceRecgMark", 0);
                        int qpSn = sp.getInt("qpSn", 0);
                        String photoUrl = sp.getString("photoUrl", "");
                        LogUtils.e("测试", "onItemClick: +SubAccidentTrainingActivity" + photoUrl);
                        bundle.putString("orgId", orgId);
                        bundle.putString("statYm", "");
                        bundle.putInt("minPer", 100);
                        bundle.putInt("photoMark", 1);
                        bundle.putInt("signMark", 1);
                        bundle.putInt("learnsn", learnsn);
                        bundle.putInt("faceRecgMark", 0);
                        bundle.putString("photoUrl", photoUrl);
                        bundle.putInt("qpSn", qpSn);
                        startActivity(AccidentTrainingCourseDetalisActivity.class, bundle);


                    } else if (accidenttrainingAdapter.getItem(position).getMark() == 1) {
                        TiShiDialog.Builder builder1 = new TiShiDialog.Builder(SubAccidentTrainingActivity.this);
                        builder1.setMessage("您已学习了该课程，继续浏览课程将不记录时间");
                        builder1.setTitle("提示:");
                        builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                int tcSn = accidenttrainingAdapter.getItem(position).getTcSn();
                                Bundle bundle = new Bundle();
                                bundle.putInt("tcsn", tcSn);
                                startActivity(SpecifiedLearningCourseDetailsAgainActivity.class, bundle);
                                //设置你的操作事项
                            }
                        });
                        builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();

                            }
                        });
                        builder1.create().show();

                    }
                }

            }
        });
    }

    @Override
    public void requestData() {
        Bundle bundle = getIntent().getExtras();
        String orgId = bundle.getString("orgId");
        int sn = bundle.getInt("learnsn");
        int qpSn = bundle.getInt("qpSn");
        int otpSn = bundle.getInt("otpSn");
        int checkMark = bundle.getInt("checkMark");
        int finishMark = bundle.getInt("finishMark");
        SharedPreferences sp = getSharedPreferences("SubAccidentTrainingActivity",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("orgId", orgId);
        edit.putInt("learnsn", sn);
        edit.putInt("qpSn", qpSn);
        edit.putInt("checkMark", checkMark);
        edit.putInt("finishMark", finishMark);
        edit.commit();
        LoginUserBean userInfo = UserManger.getUserInfo();
        LoginUserBean.DataBean data = userInfo.getData();
        String userId = data.getUserId();
        showLoadingDialog("");
        new Login().getSpecifiedLearningcourser(userId, sn, otpSn + "", this, 1);
    }

    @Override
    protected void onResume() {
        requestData();
        super.onResume();
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        if (what == 1) {
            dismissLoadingDialog();
            SpecifiedLearningCourseBean learnCourseBean = JSON.parseObject(result, SpecifiedLearningCourseBean.class);
            List<SpecifiedLearningCourseBean.RowsBean> rows = learnCourseBean.getRows();
            if (rows.size() == 0) {
                lv_course.setEmptyView(empty_view);
                return;
            }
            courseList.clear();
            courseList.addAll(rows);
            accidenttrainingAdapter = new AccidenttrainingAdapter(SubAccidentTrainingActivity.this, courseList, R.layout.specified_course_item);
            lv_course.setAdapter(accidenttrainingAdapter);
        } else if (what == 2) {
            //  showLoadingDialog("");
            SharedPreferences sp = getSharedPreferences("SubAccidentTrainingActivity",
                    Context.MODE_PRIVATE);
            String orgId = sp.getString("orgId", "");
            int learnsn = sp.getInt("learnsn", 0);
            int qpSn = sp.getInt("qpSn", 0);
            //      int sn = sp.getInt("sn", 0);
            int tcSn = sp.getInt("tcSn", 0);
            int otcSn = sp.getInt("otcSn", 0);
            //      int readLength = sp.getInt("readLength", 0);
            String photoUrl = sp.getString("photoUrl", "");
            String courseStr = sp.getString("listCourse", "");
            Gson gson = new Gson();
            List<SpecifiedLearningCourseBean.RowsBean> CourseDetailList = gson.fromJson(courseStr, new TypeToken<List<SpecifiedLearningCourseBean.RowsBean>>() {
            }.getType());
            Bundle bundle = new Bundle();
            bundle.putInt("tcsn", tcSn);
            bundle.putInt("otcSn", otcSn);
            bundle.putInt("sn", 0);//用户课程序列号
            bundle.putInt("readLength", 0);//保存时间
            bundle.putSerializable("CourseDetailList", (Serializable) CourseDetailList);
            bundle.putString("orgId", orgId);
            bundle.putString("statYm", "");
            bundle.putInt("minPer", 100);
            bundle.putInt("photoMark", 1);
            bundle.putInt("signMark", 1);
            bundle.putInt("learnsn", learnsn);
            bundle.putInt("faceRecgMark", 0);
            bundle.putString("photoUrl", photoUrl);
            bundle.putInt("qpSn", qpSn);
            startActivity(AccidentTrainingCourseDetalisActivity.class, bundle);

        }
        super.onSuccess(result, call, response, what);
    }


    @Override
    public void onError(String result, Call call, Response response, int what) {
        if (what == 2) {
            CheckBean checkBean = JSON.parseObject(result, CheckBean.class);
            String msg = checkBean.getMsg();
            TiShiDialog.Builder builder1 = new TiShiDialog.Builder(this);
            builder1.setMessage(msg);
            builder1.setTitle("提示:");
            builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    ;
                    //设置你的操作事项
                }
            });
            builder1.create().show();
        }
        super.onError(result, call, response, what);
    }

    public class AccidenttrainingAdapter extends CommonAdapter<SpecifiedLearningCourseBean.RowsBean> {
        private Context context;
        private List<SpecifiedLearningCourseBean.RowsBean> mList;
        private PopupWindow mPopWindow;


        public AccidenttrainingAdapter(Context context, List<SpecifiedLearningCourseBean.RowsBean> mList, int itemLayoutId) {
            super(context, mList, itemLayoutId);
            this.context = context;
            this.mList = mList;
        }


        private void initphotodata(final int sn) {
            if (sn == 0) {
                return;
            }
            OkGo.get(AppInterfaceConfig.BASE_URL + "course/userTcPhoto/findTc/" + sn)     // 请求方式和请求url
                    .tag(this)                       // 请求的 tag, 主要用于取消对应的请求
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(String result, Call call, Response response) {
                            SpecifiedPhotoBean specifiedPhotoBean = JSON.parseObject(result, SpecifiedPhotoBean.class);
                            List<SpecifiedPhotoBean.RowsBean> rows = specifiedPhotoBean.getRows();

                            if (rows.size() == 0) {
                                SharedPreferences sp = getSharedPreferences("SubAccidentTrainingActivity",
                                        Context.MODE_PRIVATE);
                                SharedPreferences.Editor edit = sp.edit();
                                edit.putString("photourl" + sn, "");
                                edit.commit();
                                return;
                            }
                            if (rows.size() == 1) {
                                String url = specifiedPhotoBean.getRows().get(0).getUrl();
                                if (url == null) {
                                    SharedPreferences sp = getSharedPreferences("SubAccidentTrainingActivity",
                                            Context.MODE_PRIVATE);
                                    SharedPreferences.Editor edit = sp.edit();
                                    edit.putString("photourl" + sn, "");
                                    edit.commit();

                                } else {
                                    SharedPreferences sp = getSharedPreferences("SubAccidentTrainingActivity",
                                            Context.MODE_PRIVATE);
                                    SharedPreferences.Editor edit = sp.edit();
                                    edit.putString("photourl" + sn, url);
                                    edit.commit();
                                }
                            } else if (rows.size() == 2) {
                                String url = specifiedPhotoBean.getRows().get(0).getUrl();
                                String url1 = specifiedPhotoBean.getRows().get(1).getUrl();
                                if (url == null) {
                                    SharedPreferences sp = getSharedPreferences("SubAccidentTrainingActivity",
                                            Context.MODE_PRIVATE);
                                    SharedPreferences.Editor edit = sp.edit();
                                    edit.putString("photourl" + sn, "");
                                    edit.commit();
                                } else {
                                    SharedPreferences sp = getSharedPreferences("SubAccidentTrainingActivity",
                                            Context.MODE_PRIVATE);
                                    SharedPreferences.Editor edit = sp.edit();
                                    edit.putString("photourl" + sn, url);
                                    edit.commit();
                                    if (url1 == null) {
                                        SharedPreferences sp1 = getSharedPreferences("SubAccidentTrainingActivity",
                                                Context.MODE_PRIVATE);
                                        SharedPreferences.Editor edit1 = sp1.edit();
                                        edit1.putString("photourl1" + sn, "");
                                        edit1.commit();
                                    } else {
                                        SharedPreferences sp1 = getSharedPreferences("SubAccidentTrainingActivity",
                                                Context.MODE_PRIVATE);
                                        SharedPreferences.Editor edit1 = sp1.edit();
                                        edit1.putString("photourl1" + sn, url1);
                                        edit1.commit();
                                    }
                                }

                            } else if (rows.size() == 3) {
                                String url = specifiedPhotoBean.getRows().get(0).getUrl();
                                String url1 = specifiedPhotoBean.getRows().get(1).getUrl();
                                String url2 = specifiedPhotoBean.getRows().get(2).getUrl();
                                if (url == null) {
                                    SharedPreferences sp = getSharedPreferences("SubAccidentTrainingActivity",
                                            Context.MODE_PRIVATE);
                                    SharedPreferences.Editor edit = sp.edit();
                                    edit.putString("photourl" + sn, "");
                                    edit.commit();
                                } else {
                                    SharedPreferences sp = getSharedPreferences("SubAccidentTrainingActivity",
                                            Context.MODE_PRIVATE);
                                    SharedPreferences.Editor edit = sp.edit();
                                    edit.putString("photourl" + sn, url);
                                    edit.commit();
                                    if (url1 == null) {
                                        SharedPreferences sp1 = getSharedPreferences("SubAccidentTrainingActivity",
                                                Context.MODE_PRIVATE);
                                        SharedPreferences.Editor edit1 = sp1.edit();
                                        edit1.putString("photourl1" + sn, "");
                                        edit1.commit();
                                    } else {
                                        SharedPreferences sp1 = getSharedPreferences("SubAccidentTrainingActivity",
                                                Context.MODE_PRIVATE);
                                        SharedPreferences.Editor edit1 = sp1.edit();
                                        edit1.putString("photourl1" + sn, url1);
                                        edit1.commit();
                                        if (url2 == null) {
                                            SharedPreferences sp2 = getSharedPreferences("SubAccidentTrainingActivity",
                                                    Context.MODE_PRIVATE);
                                            SharedPreferences.Editor edit2 = sp2.edit();
                                            edit2.putString("photourl2" + sn, "");
                                            edit2.commit();
                                        } else {
                                            SharedPreferences sp2 = getSharedPreferences("SubAccidentTrainingActivity",
                                                    Context.MODE_PRIVATE);
                                            SharedPreferences.Editor edit2 = sp2.edit();
                                            edit2.putString("photourl2" + sn, url2);
                                            edit2.commit();
                                        }

                                    }
                                }

                            } else if (rows.size() == 4) {
                                String url = specifiedPhotoBean.getRows().get(0).getUrl();
                                String url1 = specifiedPhotoBean.getRows().get(1).getUrl();
                                String url2 = specifiedPhotoBean.getRows().get(2).getUrl();
                                String url3 = specifiedPhotoBean.getRows().get(3).getUrl();
                                if (url == null) {
                                    SharedPreferences sp = getSharedPreferences("SubAccidentTrainingActivity",
                                            Context.MODE_PRIVATE);
                                    SharedPreferences.Editor edit = sp.edit();
                                    edit.putString("photourl" + sn, "");
                                    edit.commit();
                                } else {
                                    SharedPreferences sp = getSharedPreferences("SubAccidentTrainingActivity",
                                            Context.MODE_PRIVATE);
                                    SharedPreferences.Editor edit = sp.edit();
                                    edit.putString("photourl" + sn, url);
                                    edit.commit();
                                    if (url1 == null) {
                                        SharedPreferences sp1 = getSharedPreferences("SubAccidentTrainingActivity",
                                                Context.MODE_PRIVATE);
                                        SharedPreferences.Editor edit1 = sp1.edit();
                                        edit1.putString("photourl1" + sn, "");
                                        edit1.commit();
                                    } else {
                                        SharedPreferences sp1 = getSharedPreferences("SubAccidentTrainingActivity",
                                                Context.MODE_PRIVATE);
                                        SharedPreferences.Editor edit1 = sp1.edit();
                                        edit1.putString("photourl1" + sn, url1);
                                        edit1.commit();
                                        if (url2 == null) {
                                            SharedPreferences sp2 = getSharedPreferences("SubAccidentTrainingActivity",
                                                    Context.MODE_PRIVATE);
                                            SharedPreferences.Editor edit2 = sp2.edit();
                                            edit2.putString("photourl2" + sn, "");
                                            edit2.commit();
                                        } else {
                                            SharedPreferences sp2 = getSharedPreferences("SubAccidentTrainingActivity",
                                                    Context.MODE_PRIVATE);
                                            SharedPreferences.Editor edit2 = sp2.edit();
                                            edit2.putString("photourl2" + sn, url2);
                                            edit2.commit();

                                            if (url3 == null) {
                                                SharedPreferences sp3 = getSharedPreferences("SubAccidentTrainingActivity",
                                                        Context.MODE_PRIVATE);
                                                SharedPreferences.Editor edit3 = sp3.edit();
                                                edit3.putString("photourl3" + sn, "");
                                                edit3.commit();
                                            } else {
                                                SharedPreferences sp3 = getSharedPreferences("SubAccidentTrainingActivity",
                                                        Context.MODE_PRIVATE);
                                                SharedPreferences.Editor edit3 = sp3.edit();
                                                edit3.putString("photourl3" + sn, url3);
                                                edit3.commit();
                                                ;
                                            }


                                        }

                                    }
                                }

                            } else if (rows.size() >= 5) {
                                String url = specifiedPhotoBean.getRows().get(0).getUrl();
                                String url1 = specifiedPhotoBean.getRows().get(1).getUrl();
                                String url2 = specifiedPhotoBean.getRows().get(2).getUrl();
                                String url3 = specifiedPhotoBean.getRows().get(3).getUrl();
                                String url4 = specifiedPhotoBean.getRows().get(4).getUrl();
                                if (url == null) {
                                    SharedPreferences sp = getSharedPreferences("SubAccidentTrainingActivity",
                                            Context.MODE_PRIVATE);
                                    SharedPreferences.Editor edit = sp.edit();
                                    edit.putString("photourl" + sn, "");
                                    edit.commit();
                                } else {
                                    SharedPreferences sp = getSharedPreferences("SubAccidentTrainingActivity",
                                            Context.MODE_PRIVATE);
                                    SharedPreferences.Editor edit = sp.edit();
                                    edit.putString("photourl" + sn, url);
                                    edit.commit();
                                    if (url1 == null) {
                                        SharedPreferences sp1 = getSharedPreferences("SubAccidentTrainingActivity",
                                                Context.MODE_PRIVATE);
                                        SharedPreferences.Editor edit1 = sp1.edit();
                                        edit1.putString("photourl1" + sn, "");
                                        edit1.commit();
                                    } else {
                                        SharedPreferences sp1 = getSharedPreferences("SubAccidentTrainingActivity",
                                                Context.MODE_PRIVATE);
                                        SharedPreferences.Editor edit1 = sp1.edit();
                                        edit1.putString("photourl1" + sn, url1);
                                        edit1.commit();
                                        if (url2 == null) {
                                            SharedPreferences sp2 = getSharedPreferences("SubAccidentTrainingActivity",
                                                    Context.MODE_PRIVATE);
                                            SharedPreferences.Editor edit2 = sp2.edit();
                                            edit2.putString("photourl2" + sn, "");
                                            edit2.commit();
                                        } else {
                                            SharedPreferences sp2 = getSharedPreferences("SubAccidentTrainingActivity",
                                                    Context.MODE_PRIVATE);
                                            SharedPreferences.Editor edit2 = sp2.edit();
                                            edit2.putString("photourl2" + sn, url2);
                                            edit2.commit();

                                            if (url3 == null) {
                                                SharedPreferences sp3 = getSharedPreferences("SubAccidentTrainingActivity",
                                                        Context.MODE_PRIVATE);
                                                SharedPreferences.Editor edit3 = sp3.edit();
                                                edit3.putString("photourl3" + sn, "");
                                                edit3.commit();
                                            } else {
                                                SharedPreferences sp3 = getSharedPreferences("SubAccidentTrainingActivity",
                                                        Context.MODE_PRIVATE);
                                                SharedPreferences.Editor edit3 = sp3.edit();
                                                edit3.putString("photourl3" + sn, url3);
                                                edit3.commit();
                                                ;
                                                if (url4 == null) {
                                                    SharedPreferences sp4 = getSharedPreferences("SubAccidentTrainingActivity",
                                                            Context.MODE_PRIVATE);
                                                    SharedPreferences.Editor edit4 = sp4.edit();
                                                    edit4.putString("photourl4" + sn, "");
                                                    edit4.commit();
                                                } else {
                                                    SharedPreferences sp4 = getSharedPreferences("SubAccidentTrainingActivity",
                                                            Context.MODE_PRIVATE);
                                                    SharedPreferences.Editor edit4 = sp4.edit();
                                                    edit4.putString("photourl4" + sn, url4);
                                                    edit4.commit();
                                                }
                                            }
                                        }
                                    }
                                }

                            }
                        }

                        @Override
                        public void onError(Call call, Response response, Exception e) {
                            super.onError(call, response, e);
                            //  showToast("网络连接错误");
                        }
                    });

        }


        @Override
        public void convert(ViewHolder holder, final SpecifiedLearningCourseBean.RowsBean item, final int positon) {

            TextView tv_ttName = holder.getView(R.id.tv_ttName);
            TextView tv_courseLength = holder.getView(R.id.tv_courseLength);
            TextView tv_creationDate = holder.getView(R.id.tv_creationDate);
            TextView tv_courseName = holder.getView(R.id.tv_courseName);
            TextView tv_learnyesorno = holder.getView(R.id.tv_parent4);
            TextView tv_buxue = holder.getView(R.id.tv_parent8);
            //  TextView tv_supermarket = holder.getView(R.id.tv_parent5);
            TextView tv_learnphoto = holder.getView(R.id.tv_learnphoto);//学习照片
            final TextView tv_learn = holder.getView(R.id.tv_learn);

            //     int otcSn = mList.get(positon).getOtcSn();
            long creationDate = mList.get(positon).getCreationDate();
            int mark = mList.get(positon).getMark();
            int muMark = mList.get(positon).getMuMark();
            String ttName = mList.get(positon).getTtName();
            String courseName = mList.get(positon).getCourseName();
            int courseLength = mList.get(positon).getCourseLength();
            int readLength = mList.get(positon).getReadLength();
            final int sn = mList.get(positon).getSn();
            if (sn == 0) {
                initphotodata(0);
            } else {
                initphotodata(sn);
            }
            if (ttName == null) {
                tv_ttName.setText("");
            } else {
                tv_ttName.setText(ttName);
            }
            if (courseName == null) {
                tv_courseName.setText("");
            } else {
                tv_courseName.setText(courseName);
            }
            if (courseLength == 0) {
                tv_courseLength.setText("分钟");
            } else {
                tv_courseLength.setText(courseLength + "分钟");
            }

            SharedPreferences sp1 = getSharedPreferences("SubAccidentTrainingActivity",
                    Context.MODE_PRIVATE);
            int checkMark = sp1.getInt("checkMark", 0);
            int finishMark = sp1.getInt("finishMark", 0);
            if (finishMark == 0) {
                if (mark == 1) {
                    tv_learnyesorno.setVisibility(View.VISIBLE);
                    tv_learnyesorno.setText("已学习");
                    tv_learnyesorno.setBackgroundResource(R.drawable.shape_specified_yes);
                    tv_learn.setText("重新学习");
                    tv_learnphoto.setVisibility(View.VISIBLE);

                } else if (mark == 0) {
                    if (readLength > 0) {
                        tv_learnyesorno.setVisibility(View.VISIBLE);
                        tv_learnyesorno.setText("未学完");
                        tv_learnyesorno.setBackgroundResource(R.drawable.shape_specified_noorange);
                        tv_learn.setText("继续学习");
                        tv_learnphoto.setVisibility(View.VISIBLE);


                    } else {
                        tv_learnyesorno.setVisibility(View.VISIBLE);
                        tv_learnyesorno.setText("未学习");
                        tv_learnyesorno.setBackgroundResource(R.drawable.shape_specified_nored);
                        tv_learn.setText("开始学习");
                        tv_learnphoto.setVisibility(View.GONE);
                    }
                }
            } else {
                if (checkMark == 0) {
                    if (mark == 1) {
                        tv_learnyesorno.setVisibility(View.VISIBLE);
                        tv_learnyesorno.setText("已学习");
                        tv_learnyesorno.setBackgroundResource(R.drawable.shape_specified_yes);
                        tv_learn.setText("重新学习");
                        tv_learnphoto.setVisibility(View.VISIBLE);


                    } else if (mark == 0) {
                        if (readLength > 0) {
                            tv_learnyesorno.setVisibility(View.VISIBLE);
                            tv_learnyesorno.setText("未学完");
                            tv_learnyesorno.setBackgroundResource(R.drawable.shape_specified_noorange);
                            tv_learn.setText("继续学习");
                            tv_learnphoto.setVisibility(View.VISIBLE);

                        } else {
                            tv_learnyesorno.setVisibility(View.VISIBLE);
                            tv_learnyesorno.setText("未学习");
                            tv_learnyesorno.setBackgroundResource(R.drawable.shape_specified_nored);
                            tv_learn.setText("开始学习");
                            tv_learnphoto.setVisibility(View.GONE);
                        }
                    }

                } else {
                    if (mark == 1) {
                        tv_learnyesorno.setVisibility(View.VISIBLE);
                        tv_learnyesorno.setText("已学习");
                        tv_learnyesorno.setBackgroundResource(R.drawable.shape_specified_yes);
                        tv_learn.setVisibility(View.INVISIBLE);
                        tv_learnphoto.setVisibility(View.VISIBLE);

                    } else if (mark == 0) {

                        if (readLength > 0) {
                            tv_learnyesorno.setVisibility(View.VISIBLE);
                            tv_learnyesorno.setText("未学完");
                            tv_learnyesorno.setBackgroundResource(R.drawable.shape_specified_noorange);
                            tv_learn.setVisibility(View.VISIBLE);
                            tv_learn.setText("继续学习");
                            tv_learnphoto.setVisibility(View.VISIBLE);

                        } else {
                            tv_learnyesorno.setVisibility(View.VISIBLE);
                            tv_learnyesorno.setText("未学习");
                            tv_learnyesorno.setBackgroundResource(R.drawable.shape_specified_nored);
                            tv_learn.setVisibility(View.VISIBLE);
                            tv_learn.setText("开始学习");
                            tv_learnphoto.setVisibility(View.GONE);
                        }

                    }
                }
            }

            if (muMark == 1) {
                tv_buxue.setVisibility(View.VISIBLE);

            } else {
                tv_buxue.setVisibility(View.GONE);
            }
//            if (otcSn != 0 && otcSn > 0) {
//                tv_supermarket.setVisibility(View.GONE);
//            } else {
//                tv_supermarket.setVisibility(View.VISIBLE);
//            }
            tv_learnphoto.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Utils.isFastClicker()) {
                        return;
                    }
                    SharedPreferences sp = getSharedPreferences("SubAccidentTrainingActivity",
                            Context.MODE_PRIVATE);
                    String photourl = sp.getString("photourl" + sn, "");
                    String photourl1 = sp.getString("photourl1" + sn, "");
                    String photourl2 = sp.getString("photourl2" + sn, "");
                    String photourl3 = sp.getString("photourl3" + sn, "");
                    String photourl4 = sp.getString("photourl4" + sn, "");
                    if (photourl.equals("")) {
                        TiShiDialog.Builder builder1 = new TiShiDialog.Builder(SubAccidentTrainingActivity.this);
                        builder1.setMessage("您在本课程没有学习照片。");
                        builder1.setTitle("提示:");
                        builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                ;
                                //设置你的操作事项
                            }
                        });
                        builder1.create().show();
                    } else {
                        View contentView = LayoutInflater.from(SubAccidentTrainingActivity.this).inflate(R.layout.popwindow_showphoto_layout, null);
                        mPopWindow = new PopupWindow(contentView,
                                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
                        mPopWindow.showAtLocation(tv_title, Gravity.CENTER, 0, 0);
                        TextView tv_dismiss = (TextView) contentView.findViewById(R.id.tv_dismiss);
                        tv_dismiss.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mPopWindow.dismiss();
                            }
                        });
                        final ImageView img_photo1 = (ImageView) contentView.findViewById(R.id.img_photo1);
                        final ImageView img_photo2 = (ImageView) contentView.findViewById(R.id.img_photo2);
                        final ImageView img_photo3 = (ImageView) contentView.findViewById(R.id.img_photo3);
                        final ImageView img_photo4 = (ImageView) contentView.findViewById(R.id.img_photo4);
                        final ImageView img_photo5 = (ImageView) contentView.findViewById(R.id.img_photo5);
                        if (photourl.contains("aliyuncs.com")) {
                            Picasso.with(context).load(Uri.parse(photourl)).placeholder(R.mipmap.no_data).into(img_photo1);
                        } else {

                            Picasso.with(context).load(Uri.parse(AppInterfaceConfig.getRequestUrl(photourl))).placeholder(R.mipmap.no_data).into(img_photo1);
                        }
                        if (!photourl1.equals("")) {
                            if (photourl1.contains("aliyuncs.com")) {
                                Picasso.with(context).load(Uri.parse(photourl1)).placeholder(R.mipmap.no_data).into(img_photo2);
                            } else {

                                Picasso.with(context).load(Uri.parse(AppInterfaceConfig.getRequestUrl(photourl1))).placeholder(R.mipmap.no_data).into(img_photo2);
                            }
                        } else {
                            img_photo2.setImageResource(R.drawable.normalphoto);
                        }
                        if (!photourl2.equals("")) {
                            if (photourl2.contains("aliyuncs.com")) {
                                Picasso.with(context).load(Uri.parse(photourl2)).placeholder(R.mipmap.no_data).into(img_photo3);
                            } else {

                                Picasso.with(context).load(Uri.parse(AppInterfaceConfig.getRequestUrl(photourl2))).placeholder(R.mipmap.no_data).into(img_photo3);
                            }
                        } else {
                            img_photo3.setImageResource(R.drawable.normalphoto);
                        }
                        if (!photourl3.equals("")) {
                            if (photourl3.contains("aliyuncs.com")) {
                                Picasso.with(context).load(Uri.parse(photourl3)).placeholder(R.mipmap.no_data).into(img_photo4);
                            } else {

                                Picasso.with(context).load(Uri.parse(AppInterfaceConfig.getRequestUrl(photourl3))).placeholder(R.mipmap.no_data).into(img_photo4);
                            }
                        } else {
                            img_photo4.setImageResource(R.drawable.normalphoto);
                        }
                        if (!photourl4.equals("")) {
                            if (photourl4.contains("aliyuncs.com")) {
                                Picasso.with(context).load(Uri.parse(photourl4)).placeholder(R.mipmap.no_data).into(img_photo5);
                            } else {

                                Picasso.with(context).load(Uri.parse(AppInterfaceConfig.getRequestUrl(photourl4))).placeholder(R.mipmap.no_data).into(img_photo5);
                            }
                        } else {
                            img_photo5.setImageResource(R.drawable.normalphoto);
                        }
                    }
                }
            });
            tv_learn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //检测是否有写的权限
                    int permission = ActivityCompat.checkSelfPermission(SubAccidentTrainingActivity.this,
                            "android.permission.WRITE_EXTERNAL_STORAGE");
                    if (permission != PackageManager.PERMISSION_GRANTED) {
                        // 没有写的权限，去申请写的权限，会弹出对话框
                        ActivityCompat.requestPermissions(SubAccidentTrainingActivity.this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
                        return;
                    }
                    if (!CameraCanUseUtils.isCameraCanUse()) {

                        ToastUtil.showShortToast(SubAccidentTrainingActivity.this, "请到手机设置界面里找驾运宝允许开启照相");
                        return;
                    }

                    String learnstr = tv_learn.getText().toString().trim();
                    if (learnstr.equals("重新学习")) {
                        TiShiDialog.Builder builder = new TiShiDialog.Builder(SubAccidentTrainingActivity.this);
                        builder.setMessage("你确定要重新学习该课程吗?");
                        builder.setTitle("提示:");
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                SharedPreferences sp1 = getSharedPreferences("SubAccidentTrainingActivity",
                                        Context.MODE_PRIVATE);
                                int utchSn = sp1.getInt("learnsn", 0);
                                //            int sn = mList.get(positon).getSn();
                                new Login().getUserRelearn(utchSn, sn, (HttpListener) context, 2);
                                int tcSn = mList.get(positon).getTcSn();
                                int otcSn = mList.get(positon).getOtcSn();
                                //               int readLength = mList.get(positon).getReadLength();
                                courseList1.clear();
                                for (int i = 0; i < courseList.size(); i++) {
                                    if (courseList.get(i).getOtcSn() != otcSn && courseList.get(i).getMark() != 1) {
                                        courseList1.add(courseList.get(i));
                                    }

                                }
                                SharedPreferences.Editor edit = sp1.edit();
                                //        edit.putInt("sn",sn);
                                edit.putInt("tcSn", tcSn);
                                edit.putInt("otcSn", otcSn);
                                //                                   edit.putInt("readLength",0);
                                Gson gson = new Gson();
                                String courseStr = gson.toJson(courseList1); //将List转换成Json
                                edit.putString("listCourse", courseStr); //存入json串
                                edit.commit();
//                                    int tcSn = mList.get(positon).getTcSn();
//                                    Bundle bundle = new Bundle();
//                                    bundle.putInt("utchSn", utchSn);
//                                    bundle.putInt("sn", sn);
//                                    bundle.putInt("tcSn", tcSn);
//                                    bundle.putInt("minPer", 100);
//                                    bundle.putInt("photoMark", 1);
//                                    bundle.putInt("faceRecgMark", 0);
//                                    bundle.putString("photoUrl", "");
//                                    startActivity(NewSpecifiedAndCourseDetailsActivity.class, bundle);

                            }
                        });

                        builder.setNegativeButton("取消",
                                new android.content.DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                });


                        builder.create().show();
                    } else if (learnstr.equals("开始学习") || learnstr.equals("继续学习")) {
                        SharedPreferences sp = getSharedPreferences("SubAccidentTrainingActivity",
                                Context.MODE_PRIVATE);
                        String orgId = sp.getString("orgId", "");
                        int learnsn = sp.getInt("learnsn", 0);
                        int qpSn = sp.getInt("qpSn", 0);
                        String photoUrl = sp.getString("photoUrl", "");
                        showLoadingDialog("");
                        int tcSn = mList.get(positon).getTcSn();
                        int otcSn = mList.get(positon).getOtcSn();
                        int sn = mList.get(positon).getSn();
                        int readLength = mList.get(positon).getReadLength();
                        courseList1.clear();
                        for (int i = 0; i < courseList.size(); i++) {
                            if (courseList.get(i).getOtcSn() != otcSn && courseList.get(i).getMark() != 1) {
                                courseList1.add(courseList.get(i));
                            }

                        }
                        Bundle bundle = new Bundle();
                        bundle.putInt("tcsn", tcSn);
                        bundle.putInt("otcSn", otcSn);
                        bundle.putInt("sn", sn);//用户课程序列号
                        bundle.putInt("readLength", readLength);//保存时间
                        bundle.putSerializable("CourseDetailList", (Serializable) courseList1);
                        bundle.putString("orgId", orgId);
                        bundle.putString("statYm", "");
                        bundle.putInt("minPer", 100);
                        bundle.putInt("photoMark", 1);
                        bundle.putInt("signMark", 1);
                        bundle.putInt("learnsn", learnsn);
                        bundle.putInt("faceRecgMark", 0);
                        bundle.putString("photoUrl", photoUrl);
                        bundle.putInt("qpSn", qpSn);
                        startActivity(AccidentTrainingCourseDetalisActivity.class, bundle);

                    }

                }
            });

            if (creationDate > 0) {
                String begin = "";
                try {
                    //yyyy表示年MM表示月dd表示日
                    //yyyy-MM-dd是日期的格式，比如2015-12-12如果你要得到2015年12月12日就换成yyyy年MM月dd日
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
                    //进行格式化
                    begin = sdf.format(creationDate);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                tv_creationDate.setVisibility(View.VISIBLE);
                tv_creationDate.setText("学习时间: " + begin);

            } else {
                tv_creationDate.setVisibility(View.GONE);
            }

        }


    }
}
